package cn.gx.kevin.service.impl;

import cn.gx.kevin.dao.IDictDao;
import cn.gx.kevin.domain.Dict;
import cn.gx.kevin.service.IDictService;
import cn.gx.kevin.dao.IBaseDao;
import cn.gx.kevin.dao.IDynamicDao;
import cn.gx.kevin.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Generate by Kevin's CodeBuilder
 **/
@Service
public class DictService extends BaseService<Dict, String> implements IDictService {

    private static final long serialVersionUID = -1L;

    /*****测试动态dao的使用******/
    @Autowired
    private IDynamicDao dynamicDao;

    private IDictDao dictDao;

    @Resource(name = "dictDao")
    @Override
    public void setDao(IBaseDao<Dict, String> dao) {
        this.dao = dao;
        dictDao = (IDictDao) dao;
    }

    @Override
    public int insert(Dict entity) throws Exception {
        int r = super.insert(entity);
        return r;
    }

    @Override
    public int update(Dict entity) throws Exception {
        return super.update(entity);
    }

    @Override
    public List<Dict> selectParam(Map<String, Object> param) throws Exception {


        /*******统计方式分页*******/

       /* DynamicComputedParam computedPaginParam = new DynamicComputedParam();
        computedPaginParam.setTabelName("t_test_data");
        computedPaginParam.setComputeType("count");
        computedPaginParam.setGroupByFeild("mystr");

        LinkedList<DynamicWhereParam> whereLists = new LinkedList<>();
        DynamicWhereParam param1 = new DynamicWhereParam();
        param1.setJdbcType("Date");
        param1.setSqlOp(">=");
        param1.setFieldName("mydate");
        param1.setFieldValue(DateUtils.computedDateByNow(new Date(),-5,"m"));
        whereLists.add(param1);
        computedPaginParam.setWhereValues(whereLists);


        DynamicPagingResult pagingResult = this.dynamicDao.selectPaginComputed(1,8,computedPaginParam);

        System.out.println(JSONObject.toJSONString(pagingResult));

        pagingResult = this.dynamicDao.selectPaginComputed(2,8,computedPaginParam);

        System.out.println(JSONObject.toJSONString(pagingResult));

        pagingResult = this.dynamicDao.selectPaginComputed(3,8,computedPaginParam);

        System.out.println(JSONObject.toJSONString(pagingResult));

        pagingResult = this.dynamicDao.selectPaginComputed(4,8,computedPaginParam);

        System.out.println(JSONObject.toJSONString(pagingResult));*/

        /***********  分页查询 *************/
      /*   LinkedList<DynamicWhereParam> whereList = new LinkedList<DynamicWhereParam>();
       DynamicWhereParam p3 = new DynamicWhereParam();
        p3.setContact(DynamicDao.where_sql_and);
        p3.setFieldName("mydate");
        p3.setSqlOp(DynamicDao.where_sql_great_eqauls);
        p3.setFieldValue("2019-05-01 00:00:00");
        p3.setJdbcType("date");

        DynamicWhereParam p4 = new DynamicWhereParam();
        p4.setContact(DynamicDao.where_sql_and);
        p4.setFieldName("mydate");
        p4.setSqlOp(DynamicDao.where_sql_less_eqauls);
        p4.setFieldValue("2019-12-01 00:00:00");
        p4.setJdbcType("date");

        whereList.add(p3);
        whereList.add(p4);*/

       /* DynamicSelectParam params = new DynamicSelectParam();
        params.setTabelName("t_test_data");
        params.setWhereValues(whereList);
        List<String> fields = new ArrayList<>();
        fields.add("id");
        fields.add("mynum");
        fields.add("mystr");
        fields.add("mydate");
        params.setSelectFields(fields);
        DynamicPagingResult pagingResult = this.dynamicDao.selectPagin(1,2,params);

        System.out.println(JSONObject.toJSONString(pagingResult));

        pagingResult = this.dynamicDao.selectPagin(2,2,params);

        System.out.println(JSONObject.toJSONString(pagingResult));

        pagingResult = this.dynamicDao.selectPagin(3,2,params);

        System.out.println(JSONObject.toJSONString(pagingResult));*/


        /************ count / sum / avg 统计 *************/
    /*
        DynamicComputedParam computedParam = new DynamicComputedParam();
       computedParam.setTabelName("t_test_data");
       //computedParam.setGroupByFeild("mystr");
       computedParam.setComputeType("count");
        //computedParam.setComputeType("sum");
       //computedParam.setComputedFeild("mynum + mynum");

     DynamicWhereParam p1 = new DynamicWhereParam();
        p1.setContact(DynamicDao.where_sql_and);
        p1.setFieldName("mystr");
        p1.setSqlOp(DynamicDao.where_sql_in);
        List<String> plist = new ArrayList<>();
        plist.add("aaa");
        plist.add("bbb");
        plist.add("ccc");
        p1.setFieldValue(plist);

        DynamicWhereParam p3 = new DynamicWhereParam();
        p3.setContact(DynamicDao.where_sql_and);
        p3.setFieldName("mydate");
        p3.setSqlOp(DynamicDao.where_sql_great_eqauls);
        p3.setFieldValue("2019-01-01 00:00:00");

        LinkedList<DynamicWhereParam> whereList = new LinkedList<DynamicWhereParam>();
       // whereList.add(p1);
        whereList.add(p3);

        computedParam.setWhereValues(whereList);

        List<LinkedHashMap<String,Object>> list = this.dynamicDao.computedParam(computedParam);

        System.out.println(JSON.toJSONString(list));
        ****/

        /**********删除数据************/
       /* DynamicDeleteParam deleteParam = new DynamicDeleteParam();
        deleteParam.setTabelName("t_test_data");

        LinkedList<DynamicWhereParam> whereList = new LinkedList<DynamicWhereParam>();

        DynamicWhereParam p1 = new DynamicWhereParam();
        p1.setContact(DynamicDao.where_sql_and);
        p1.setFieldName("mystr");
        p1.setSqlOp(DynamicDao.where_sql_in);
        List<String> plist = new ArrayList<>();
        plist.add("new--01");
        plist.add("new");
        p1.setFieldValue(plist);

        DynamicWhereParam p2 = new DynamicWhereParam();
        p2.setContact(DynamicDao.where_sql_or);
        p2.setFieldName("mynum");
        p2.setSqlOp(DynamicDao.where_sql_less_eqauls);
        p2.setFieldValue(100);

        DynamicWhereParam p3 = new DynamicWhereParam();
        p3.setContact(DynamicDao.where_sql_and);
        p3.setFieldName("mydate");
        p3.setSqlOp(DynamicDao.where_sql_great_eqauls);
        p3.setFieldValue("2019-05-01 00:00:00");

        DynamicWhereParam p4 = new DynamicWhereParam();
        p4.setContact(DynamicDao.where_sql_and);
        p4.setFieldName("mydate");
        p4.setSqlOp(DynamicDao.where_sql_less_eqauls);
        p4.setFieldValue("2019-07-01 00:00:00");

        whereList.add(p1);
        whereList.add(p2);
        whereList.add(p3);
        whereList.add(p4);

        deleteParam.setWhereValues(whereList);

        int c = this.dynamicDao.delete(deleteParam);

        System.out.println("delete count = "+c);*/


        /******** 批量插入数据 **********/
        /*DynamicBatchInsertParam binsertParam = new DynamicBatchInsertParam();
        binsertParam.setTabelName("t_test_data");
        LinkedList<String> fieldInsers = new LinkedList<>();
        fieldInsers.add("ID");
        fieldInsers.add("mynum");
        fieldInsers.add("mystr");
        fieldInsers.add("mydate");
        fieldInsers.add("myfield");
        binsertParam.setFileds(fieldInsers);

        List<LinkedList<DynamicInsertFieldValue>> values = new ArrayList<LinkedList<DynamicInsertFieldValue>>();
        for(int i = 1 ; i < 120 ; ++i){
            LinkedList<DynamicInsertFieldValue> row = new LinkedList<DynamicInsertFieldValue>();
            row.add( new DynamicInsertFieldValue( 220500 + i));
            row.add( new DynamicInsertFieldValue( 100 + i));
            if(i % 2 == 0){
                row.add( new DynamicInsertFieldValue( "my001_"+ (i % 2) ));
            }else if(i % 3 == 0){
                row.add( new DynamicInsertFieldValue( "my009_"+ (i % 3) ));
            }else{
                row.add( new DynamicInsertFieldValue( "my99_"+ (i) ));
            }
            row.add( new DynamicInsertFieldValue(new Date() ,"date"));
            row.add( new DynamicInsertFieldValue("f_"+i));
            values.add(row);
        }
        binsertParam.setValues(values);

        int c = this.dynamicDao.insertBatch(binsertParam);

        System.out.println("c === "+c);*/


        /**********动态插入数据**********/
      /*  DynamicInsertParam p11 = new DynamicInsertParam();
        p11.setTabelName("t_test_data");
        LinkedMap<String,DynamicInsertFieldValue> filedMap1 = new LinkedMap<>();
        filedMap1.put("id",new DynamicInsertFieldValue(888));
        filedMap1.put("mynum",new DynamicInsertFieldValue(28));
        filedMap1.put("mystr",new DynamicInsertFieldValue("2228--01"));
        filedMap1.put("mydate",new DynamicInsertFieldValue(new Date(),"date"));
        filedMap1.put("myfield",new DynamicInsertFieldValue( MyUUID.getJVMUUID()));
        p11.setFields(filedMap1);
        try{
            int c = this.dynamicDao.insert(p11);
            System.out.println(c);
        }catch (Exception x){
            x.printStackTrace();
        }*/
       /*****动态查询数据-test*******/
/*        DynamicSelectParam selectParam = new DynamicSelectParam();
        selectParam.setTabelName("t_test_data");
        List<String> fields = new ArrayList<>();
        fields.add("id");
        fields.add("mynum");
        fields.add("mystr");
        fields.add("mydate");

        LinkedList<DynamicWhereParam> whereList = new LinkedList<DynamicWhereParam>();

        DynamicWhereParam p1 = new DynamicWhereParam();
        p1.setContact(DynamicDao.where_sql_and);
        p1.setFieldName("mystr");
        p1.setSqlOp(DynamicDao.getWhere_sql_notin);
        List<String> plist = new ArrayList<>();
        plist.add("abc");
        plist.add("nnn");
        p1.setFieldValue(plist);

        DynamicWhereParam p2 = new DynamicWhereParam();
        p2.setContact(DynamicDao.where_sql_or);
        p2.setFieldName("mynum");
        p2.setSqlOp(DynamicDao.where_sql_great_eqauls);
        p2.setFieldValue(100);

        DynamicWhereParam p3 = new DynamicWhereParam();
        p3.setContact(DynamicDao.where_sql_and);
        p3.setFieldName("mydate");
        p3.setSqlOp(DynamicDao.where_sql_great_eqauls);
        p3.setFieldValue("2019-08-01 00:00:00");

        DynamicWhereParam p4 = new DynamicWhereParam();
        p4.setContact(DynamicDao.where_sql_and);
        p4.setFieldName("mydate");
        p4.setSqlOp(DynamicDao.where_sql_less_eqauls);
        p4.setFieldValue("2019-12-01 00:00:00");

        whereList.add(p1);
        whereList.add(p2);
        whereList.add(p3);
        whereList.add(p4);

        selectParam.setWhereValues(whereList);
        selectParam.setSelectFields(fields);

        selectParam.setOrder("mynum","desc");
        selectParam.setOrder("mydate","asc");

        List<Map<String,Object>> res =  dynamicDao.select(selectParam);*/

        return super.selectParam(param);
    }

    @Override
    public int deleteBatch(List<String> list) throws Exception {

        return super.deleteBatch(list);
    }


}
